package com.example.download.utils
import android.Manifest
import android.content.Context
import androidx.fragment.app.FragmentManager
import permissions.dispatcher.PermissionUtils
/**
 * @author: Mr.You
 * @create: 2022-11-17 10:17
 * @description:
 **/
object PermissionTip {
    private val PERMISSION_GETNOWLOCATION = arrayOf(
        "android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"
    )

    private val PERMISSION_CHECKUPDATE: Array<String> = arrayOf(
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE"
    )

    private val PERMISSION_OPENCAMERA = arrayOf(
        "android.permission.CAMERA"
    )

    private val PERMISSION_PHOE_CALL = arrayOf(
        "android.permission.CALL_PHONE"
    )


    private var dialog: PermissionTopTipDialog? = null

    fun tip(context: Context, manager: FragmentManager, permissionType: PermissionType) {
        val isAgreement = when (permissionType) {
            PermissionType.WRITE_AND_READ -> PermissionUtils.hasSelfPermissions(
                context,
                *PERMISSION_CHECKUPDATE
            )
            PermissionType.READ_PHONE_STATE -> PermissionUtils.hasSelfPermissions(
                context,
                Manifest.permission.READ_PHONE_STATE
            )
            PermissionType.GPS -> PermissionUtils.hasSelfPermissions(
                context,
                *PERMISSION_GETNOWLOCATION
            )
            PermissionType.CAMERA -> PermissionUtils.hasSelfPermissions(
                context,
                *PERMISSION_OPENCAMERA
            )
            PermissionType.PHONE_CALL -> PermissionUtils.hasSelfPermissions(
                context,
                *PERMISSION_PHOE_CALL
            )
        }
        if (!isAgreement) {
            if(dialog!=null){
                dialog?.dismiss()
                dialog = null
            }
            dialog = PermissionTopTipDialog().apply {
                this.permissionType=permissionType
                this.show(manager, "")
            }
        }
    }


    fun hide() {
        dialog?.dismiss()
    }


}
